package com.roger.myHome.controller;

import com.roger.myHome.aspect.WebLog;
import com.roger.myHome.entity.dto.ExpensesAddReq;
import com.roger.myHome.entity.qto.ExpensesQry;
import com.roger.myHome.page.EUDataGridResult;
import com.roger.myHome.service.ExpensesService;
import com.roger.myHome.util.UserHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/expenses")
@Slf4j
public class ExpensesController {
    @Resource
    private UserHelper userHelper;

    @Resource
    private ExpensesService expensesService;

    @RequestMapping("/page")
    @ResponseBody
    @WebLog
    public EUDataGridResult page(@RequestParam(value="page",required=false) String page,
                                 @RequestParam(value="rows",required=false) String rows, HttpServletRequest req){

        ExpensesQry qryReq = new ExpensesQry();
        qryReq.setPage((Integer.parseInt(page) - 1) * Integer.parseInt(rows));
        qryReq.setRows(Integer.parseInt(rows));
        qryReq.setCreateUser(userHelper.getLoginUser(req).getId());
        qryReq.setPayDate1(req.getParameter("payDate1"));
        qryReq.setPayDate2(req.getParameter("payDate2"));
        qryReq.setContent(req.getParameter("content"));
        log.info("{}:ExpensesController page,{}",userHelper.getLoginUser(req).getLoginName(), qryReq);
        return expensesService.page(qryReq);
    }

    @PostMapping("/add")
    @WebLog
    public String add(HttpServletRequest request , ExpensesAddReq expensesAddReq){
        //System.out.println(expensesAddReq);
        Long createUser = userHelper.getLoginUser(request).getId();
        expensesAddReq.setCreateUser(createUser);
        return expensesService.add(expensesAddReq);
    }

    @DeleteMapping("/delete/{id}")
    public String delete(HttpServletRequest request, @PathVariable Long id){
        return expensesService.delete(userHelper.getLoginUser(request).getId(),id);
    }

    @PostMapping("/searchExpenseByDate")
    public String searchExpenseByDate(HttpServletRequest request,ExpensesQry expensesQry){
        //System.out.println(expensesQry);
        expensesQry.setCreateUser(userHelper.getLoginUser(request).getId());
        return expensesService.searchExpenseByDate(expensesQry);
    }

}
